package cn.iocoder.yudao.module.mini.service.order;

import cn.iocoder.yudao.module.mini.dal.mysql.address.MiniAddressMapper;
import cn.iocoder.yudao.module.mini.dal.mysql.project.ProjectMapper;
import cn.iocoder.yudao.module.mini.dal.mysql.user.MiniUserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.mini.controller.admin.order.vo.*;
import cn.iocoder.yudao.module.mini.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.mini.convert.order.OrderConvert;
import cn.iocoder.yudao.module.mini.dal.mysql.order.OrderMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mini.enums.ErrorCodeConstants.*;

/**
 * 订单 Service 实现类
 *
 * @author dzy
 */
@Service
@Validated
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private MiniUserMapper miniUserMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private MiniAddressMapper miniAddressMapper;

    @Override
    public Long createOrder(OrderCreateReqVO createReqVO) {
        // 插入
        OrderDO order = OrderConvert.INSTANCE.convert(createReqVO);
        orderMapper.insert(order);
        // 返回
        return order.getId();
    }

    @Override
    public void updateOrder(OrderUpdateReqVO updateReqVO) {
        // 校验存在
        validateOrderExists(updateReqVO.getId());
        // 更新
        OrderDO updateObj = OrderConvert.INSTANCE.convert(updateReqVO);
        orderMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrder(Long id) {
        // 校验存在
        validateOrderExists(id);
        // 删除
        orderMapper.deleteById(id);
    }

    private void validateOrderExists(Long id) {
        if (orderMapper.selectById(id) == null) {
            throw exception(ORDER_NOT_EXISTS);
        }
    }

    @Override
    public OrderDO getOrder(Long id) {
        OrderDO orderDO = orderMapper.selectById(id);
        orderDO.setCustomerName(miniUserMapper.selectById(orderDO.getCustomerId()).getNickname());
        orderDO.setTechnicianName(miniUserMapper.selectById(orderDO.getTechnicianId()).getNickname());
        orderDO.setProjectName(projectMapper.selectById(orderDO.getProjectId()).getName());
        orderDO.setAddressName(miniAddressMapper.selectById(orderDO.getAddressId()).getAddress());
        return orderDO;
    }

    @Override
    public List<OrderDO> getOrderList(Collection<Long> ids) {
        return orderMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderDO> getOrderPage(OrderPageReqVO pageReqVO) {

        PageResult<OrderDO> orderDOPageResult = orderMapper.selectPage(pageReqVO);
        orderDOPageResult.getList().forEach(item -> {
            item.setCustomerName(miniUserMapper.selectById(item.getCustomerId()).getNickname());
            item.setTechnicianName(miniUserMapper.selectById(item.getTechnicianId()).getNickname());
            item.setProjectName(projectMapper.selectById(item.getProjectId()).getName());
            item.setAddressName(miniAddressMapper.selectById(item.getAddressId()).getAddress());
        });
        return orderDOPageResult;
    }

    @Override
    public List<OrderDO> getOrderList(OrderExportReqVO exportReqVO) {
        return orderMapper.selectList(exportReqVO);
    }

}
